<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>客户列表</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/default/easyui.css"/>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/icon.css"/>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/demo.css"/>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.min.js"></script>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.easyui.min.js"></script>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/admin/css/input1.css"/>
	<script type="text/javascript">
		var aid;
		var cid;
		$(function(){
			$('#station').combobox({
				valueField:'station_id', //值字段
				textField:'station_name', //显示的字段
				url:'${pageContext.request.contextPath }/admin/station/findStationByKind/1',
				onLoadSuccess : function(data) {
		            $('#station').combobox("setValue",-1);
	        	}
	        });
	        
	       $('#state').combobox({
				valueField:'value',
				textField:'text',
				data:[{"value":"-1","text":"--- 请选择 ---"},
					{"value":"00","text":"生效"},
					{"value":"01","text":"冻结"},
					{"value":"02","text":"注销"},
					{"value":"03","text":"未开户"}
				]
			});
			$('#account_type').combobox({
				valueField:'value',
				textField:'text',
				data:[{"value":"00","text":"主账户"},
					{"value":"01","text":"次级账户"}
				]
			});
			$('#account_type').combobox("setValue","00");

			$('#property').combobox({
				valueField:'value',
				textField:'text',
				data:[{"value":"00","text":"商户"},
					{"value":"01","text":"个人"}
				]
			});
			$('#property').combobox("setValue","00");
			$('#state').combobox("setValue","-1");
			loadTable();

			$('#dlg').dialog('close');
		});
		
		function loadTable(){
			$('#dg').datagrid({
				rownumbers:true,
				singleSelect:true,
				pagination:true,
				pageSize:10,
				pageList:[10,20,30,50,100],
		 		url:'${pageContext.request.contextPath }/admin/customer/listCustomer',
		 		queryParams:{
		 			 station:$('#station').combobox("getValue"),
		 			 custname:$('#custname').val(),
		 			 mobile:$('#mobile').val(),
		 			 state:$('#state').combobox("getValue")
		 		},
	    		columns:[[
					{field:'cust_id',title:'编号',align:'center',width:45},
					{field:'cust_name',title:'客户姓名',align:'center',width:70},
					{field:'cust_sex',title:'性别',align:'center',width:40,formatter: function (value, row, index) {
						var res="";
						if(value==0){
							res="男";
						}else if(value==1){
							res= "女";
						}else{
							res="未知";
						}
						return res;
					}},
					{field:'cust_identity',title:'身份证号码',align:'center',width:140},
					{field:'mobile',title:'联系电话',align:'center',width:100},
					{field:'init_pass',title:'初始密码',align:'center',width:80},
					{field:'station_name',title:'所属站点',width:100,align:'center'},					
					{field:'is_valid',title:'是否有效',width:60,align:'center',formatter: function (value, row, index) {
						var status='';
						if(value==0)
							status="<span style='color:red'>禁用</span>";
						else if(value==1)
							status="正常";
						return status;
					}},
					{field:'state',title:'账户状态',width:60,align:'center',formatter: function (value, row, index) {
						var status='';
						status=value;
						if(value==null){
							status="<span style='color:red'>未开户</span>";
						}else if(value=="00"){
							status="生效";
						}else if(value=="01"){
							status="<span style='color:blue'>冻结</span>";
						}else if(value=="02"){
							status="<span style='color:red'>注销</span>";
						}	
						return status;
					}},
					{field:'create_time',title:'创建时间',width:130,align:'center'},
					{field:'operation',title:'操作选项',width:360,align:'left',formatter: function (value, row, index) {
						var str = '<a href="javascript:edit('+row.cust_id+')" name="edit" class="easyui-linkbutton" ></a>';
						if(row.is_valid==0){
							str=str+' | <a href="javascript:changeValid('+row.cust_id+',1)" name="on_use"  class="easyui-linkbutton" ></a>';
						}else{
							str=str+' | <a href="javascript:changeValid('+row.cust_id+',0)" name="no_use" style="color:red" class="easyui-linkbutton" ></a>';
						}					
						
						if(row.init_pass==null || row.init_pass==''){
							str=str+' | <a href="javascript:manage('+row.cust_id+',\''+row.cust_name+'\','+row.cust_sex+',\''+row.mobile+'\','+row.is_valid+')" name="manage"  class="easyui-linkbutton" ></a>';
						}else{
							str=str+' | <a href="javascript:resetPass('+row.cust_id+',\''+row.cust_name+'\')" name="resetPass"  class="easyui-linkbutton" ></a>';
						}
						if(row.state==null && row.init_pass!=null){
							str=str+' | <a href="javascript:addAccount('+row.cust_id+',\''+row.cust_name+'\','+row.cust_sex+',\''+row.mobile+'\','+row.acct_id+')" name="addAccount"  class="easyui-linkbutton" ></a>';
						}else if(row.state=="00"){
							str=str+' | <a href="javascript:changeAccount(1,'+row.acct_id+',\''+row.cust_name+'\')" name="dongjie"  class="easyui-linkbutton" ></a>';
							str=str+' | <a href="javascript:changeAccount(2,'+row.acct_id+',\''+row.cust_name+'\')" name="zhuxiao"  class="easyui-linkbutton" ></a>';
						}else if(row.state=="01"){
							str=str+' | <a href="javascript:changeAccount(3,'+row.acct_id+',\''+row.cust_name+'\')" name="jiedong"  class="easyui-linkbutton" ></a>';
							str=str+' | <a href="javascript:changeAccount(2,'+row.acct_id+',\''+row.cust_name+'\')" name="zhuxiao"  class="easyui-linkbutton" ></a>';
						}else if(row.state=="02"){
							str=str+' | <a href="javascript:changeAccount(4,'+row.acct_id+',\''+row.cust_name+'\')" name="shengxiao"  class="easyui-linkbutton" ></a>';
						}
						return str;
					}}
	 			]],
				onLoadSuccess:function(data){  
			        $("a[name='on_use']").linkbutton({text:'启用',plain:true,iconCls:'icon-my-enable'});
			        $("a[name='no_use']").linkbutton({text:'禁用',plain:true,iconCls:'icon-my-disable'});
			        $("a[name='edit']").linkbutton({text:'编辑',plain:true,iconCls:'icon-edit'}); 
			        $("a[name='manage']").linkbutton({text:'分配账号',plain:true,iconCls:'icon-my-manage'}); 
			        $("a[name='resetPass']").linkbutton({text:'重置密码',plain:true,iconCls:'icon-my-set'});
			        $("a[name='show']").linkbutton({text:'查看',plain:true,iconCls:'icon-search'}); 
			        $("a[name='addAccount']").linkbutton({text:'开户',plain:true,iconCls:'icon-my-add'}); 
			        $("a[name='dongjie']").linkbutton({text:'冻结',plain:true,iconCls:'icon-my-dongjie'});
			        $("a[name='zhuxiao']").linkbutton({text:'注销',plain:true,iconCls:'icon-my-minus'}); 
			        $("a[name='jiedong']").linkbutton({text:'解冻',plain:true,iconCls:'icon-my-jiedong'});
			        $("a[name='shengxiao']").linkbutton({text:'生效',plain:true,iconCls:'icon-ok'});
				}
			});
		}
		
		// 添加客户账户
		function addAccount(custid,custname,custsex,mobile,acctid){
			$("#cc").html(custname);
			$("#mm").html(mobile);
			cid=custid;
			aid=acctid;
			$('#dlg').dialog('open');
		}
		
		function doAddAccount(){
			 var atype=$('#account_type').combobox('getValue');
			 var aproperty=$('#property').combobox('getValue');
			 $.post("${pageContext.request.contextPath}/admin/customer/addAccount",{cust_id:cid,account_type:atype,property:aproperty},
						function(data){
							if(data=="0"){
								$.messager.alert('系统消息','<span style=\"color:red;\">客户账户开户失败，请稍后再试！</span>');
								$('#dlg').dialog('close');	 
							}else if(data=="1"){
								$.messager.alert('系统消息',"客户账号开户成功！");
								$('#dlg').dialog('close');	
								$('#dg').datagrid('reload'); 
							}	
						},"text");
		}
		
		
		// 冻结账户
		function changeAccount(method,acctid,custname){
			var opers=new Array("开通","冻结","注销","解冻","生效");
			var confirmMsg="您确定要"+opers[method]+"["+custname+"]的账户吗？";
			$.messager.confirm('系统确认',confirmMsg, function(r){
				if(r){
					var states=new Array("00","01","02","03","04");
					var state=states[method];
					$.post("${pageContext.request.contextPath}/admin/customer/changeAccountStatus",{acct_id:acctid,state:state},
						function(data){
							if(data=="0"){
								$.messager.alert('系统消息','<span style=\"color:red;\">账户'+opers[method]+'失败，请稍后再试！</span>');	 
							}else if(data=="1"){
								$.messager.alert('系统消息',"客户 ["+custname+"] 账号"+opers[method]+"成功！");
								$('#dg').datagrid('reload'); 
							}	
						},"text");
				}else{
					$.messager.alert('系统消息','<span style=\"color:green;\">'+opers[method]+'账户操作已取消！</span>');
				}
			});
		}
		
		// 为客户分配登录账号
		function manage(custid,custname,custsex,mobile,valid){
			if(valid==0){
				$.messager.alert('系统消息','<span style=\"color:red;\">客户的状态为：禁用，无法为其分配登录账号，请先更改客户状态！</span>');
				return;
			}
			var pass=getRandomPass();
			$.post("${pageContext.request.contextPath}/admin/customer/manage",{custid:custid,password:pass,custname:custname,custsex:custsex,mobile:mobile},
				function(data){
					if(data=="0"){
						$.messager.alert('系统消息','<span style=\"color:red;\">密码重置失败，请稍后再试！</span>');	 
					}else if(data=="1"){
						$.messager.alert('系统消息',"客户 ["+custname+"] 登录账号分配成功！可以使用姓名和手机号进行登录！！<br/><br/>客户 ["+custname+"] 初始登录密码为：<span style='color:red;'>"+pass+"</span><br/><br/>温馨提醒：如果遗忘重置后的初始密码，可以在用户管理列表中查询！");
						$('#dg').datagrid('reload'); 
					}else if(data=="2"){
						$.messager.alert('系统消息',"客户分配账号操作未能成功进行！<br/><br/><span style='color:red'>原因：您要操作的客户 ["+custname+"] 已经分配登录账号！</span>");
					}		
				},"text");
		}
		
		// 更新客户是否有效的状态
		function changeValid(custid,valid){
			$.post("${pageContext.request.contextPath}/admin/customer/changeValid",{custid:custid,valid:valid},
				function(data){
					if(data=="0"){
						$.messager.alert('系统消息','<span style=\"color:red;\">客户有效状态更新失败，请稍后再试！</span>');	 
					}else{
						$.messager.alert('系统消息','客户有效状态更新成功！');
						$('#dg').datagrid('reload'); 
					}
						
				},"text");
		}
		
		function showImg(imgUrl){
			$('#dlg').dialog('open');
			var image=document.getElementById("stationImg");
			var src="${pageContext.request.contextPath }"+imgUrl;
			image.setAttribute("src",src );
		}
		
		// 修改客户信息
		function edit(custid){
	//		show("${pageContext.request.contextPath}/admin/customer/editBefore/"+custid,"修改客户信息","icon-edit");
			location.href="${pageContext.request.contextPath}/admin/customer/editBefore/"+custid;
		}
		// 重置密码功能，重置的密码为随机生成的6位数字
		function resetPass(custid,custname){
			var pass=getRandomPass();
			$.post("${pageContext.request.contextPath}/admin/customer/resetPass",{custid:custid,password:pass},
				function(data){
					if(data=="0"){
						$.messager.alert('系统消息','<span style=\"color:red;\">密码重置失败，请稍后再试！</span>');	 
					}else if(data=="1"){
						$.messager.alert('系统消息',"密码重置成功！<br/><br/>客户 ["+custname+"] 重置后的密码为：<span style='color:red;'>"+pass+"</span><br/><br/>温馨提醒：如果遗忘重置后的初始密码，可以在用户管理列表中查询！");
						$('#dg').datagrid('reload'); 
					}else if(data=="2"){
						$.messager.alert('系统消息',"密码重置操作未能成功进行！<br/><br/><span style='color:red'>原因：您要操作的客户还没有分配任何登录账号！</span>");
					}		
				},"text");
		}
		
		// 得到6位数字的随机密码
		function getRandomPass(){
			var pass="";
			for(var i=0;i<6;i++){
				pass=pass+Math.ceil(Math.random()*(10)-1);
			}
			return pass;
		}

		// 打开一个新的选项卡，并制定选项卡的名称
		function show(url,name){
			parent.window.addTab(name,url);
		}
		
		// 打开一个新的选项卡，并制定选项卡的名称
		function show(url,name,icon){
			parent.window.addTab(name,url,icon);
		}
		
		function findData(){
			loadTable();
		}
		
		function doAddCust(){
			show("${pageContext.request.contextPath}/admin/customer/addBefore","新增客户信息","icon-person-add");
		}

	</script>
	<style type="text/css">
		body{
		font-family:'微软雅黑';}
	</style>
  </head>
  
  <body>
    <table id="dg" title="客户列表" style="width:1230px;height:455px" toolbar="#searchtool" loadMsg="正在查询..."></table>
    <div id="searchtool" style="padding:5px">  
       <span style="margin:0px 12px;">所属提货仓:</span><input id="station" class="easyui-textbox" style="width:160px;height:26px"/> 
       <span style="margin:0px 12px;">姓名:</span><input id="custname" class="easyui-textbox" style="width:160px;height:26px"/> 
       <span style="margin:0px 12px;">手机号码:</span><input id="mobile" class="easyui-textbox" style="width:160px;height:26px"/> 
       <span style="margin:0px 12px;">账户状态:</span><input id="state" class="easyui-textbox" style="width:160px;height:26px"/> 
       <a href="javascript:findData()" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:100px;margin:0px 3px;">查询</a> 
       <a href="javascript:doAddCust()" class="easyui-linkbutton" data-options="iconCls:'icon-person-add'" style="width:100px;margin:0px 3px;">新增客户</a> 
    </div> 
	<div id="dlg" class="easyui-dialog" title="开通客户账户" style="width:400px;height:300px;padding:10px"
			data-options="
				iconCls: 'icon-my-add',
				buttons: '#dlg-buttons'
			">
		<div>
			<div style="float:left; width:120px; height:35px; line-height:35px; vertical-align:middle;">姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名：</div>
			<div id="cc" style="float:left; width:200px; height:35px; line-height:35px; vertical-align:middle;">11</div>
			<div style="clear:left;"></div>
		</div>
		<div>
			<div style="float:left; width:120px; height:35px; line-height:35px; vertical-align:middle;">电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话：</div>
			<div id="mm" style="float:left; width:200px; height:35px; line-height:35px; vertical-align:middle;">&nbsp;</div>
			<div style="clear:left;"></div>
		</div>
		<div>
			<div style="float:left; width:120px; height:35px; line-height:35px; vertical-align:middle;">账户类型：</div>
			<div style="float:left; width:200px; height:35px; line-height:35px; vertical-align:middle;">
				<input id="account_type" class="easyui-textbox" style="width:120px;height:26px"/> 
			</div>
			<div style="clear:left;"></div>
		</div>
		<div>
			<div style="float:left; width:120px; height:35px; line-height:35px; vertical-align:middle;">账户性质：</div>
			<div style="float:left; width:200px; height:35px; line-height:35px; vertical-align:middle;">
				<input id="property" class="easyui-textbox" style="width:120px;height:26px"/>
			</div>
			<div style="clear:left;"></div>
		</div>
	</div>
	<div id="dlg-buttons">
		
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-my-add" onclick="javascript:doAddAccount()">确认开户</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-undo" onclick="javascript:$('#dlg').dialog('close')"> 取消 </a>
	</div>
  </body>
</html>
